window.onload = () => {
    // 添加切换显示模式的按钮
    const button = document.createElement("button") as HTMLButtonElement
    button.id = "darkModeButton"
    button.textContent = chrome.i18n.getMessage("enableDarkModeText")
    button.className = "header-upload-entry"
    button.setAttribute("style", "background-color:gray;")
    // button.style = "background-color:gray;"

    const input = document.createElement("input")
    input.type = "checkbox"
    input.id = "darkSetting"

    const autoApplyText = chrome.i18n.getMessage("autoApplyText")
    document.querySelector('.right-entry').prepend(button, input, autoApplyText)

    button.addEventListener('click', (ev) => {
        enableDarkMode()
    })
    input.addEventListener('click', (ev) => {
        storeSetting()
    })

    checkSetting()


    setTimeout(() => {
        chrome.runtime.sendMessage({payload: "Hello from a content"}, (response) => {
            console.log(response)
        })
    }, 300)
}

function enableDarkMode() {
    console.log("开启黑暗模式")
    const el1 = document.querySelector("#i_cecream") as HTMLElement
    el1.style.backgroundColor = "black"
    const el2 = document.querySelector(".bili-header__channel") as HTMLElement
    el2.style.backgroundColor = "black"
}

async function storeSetting() {
    const settingCheckbox = document.getElementById("darkSetting") as HTMLInputElement
    const isEnabled = settingCheckbox.checked
    console.log("isEnabled: ", isEnabled)

    const setting = {
        enabled: isEnabled
    }

    try {
        await chrome.storage.local.set(setting)
        console.log("storage.setting", setting)
    } catch (error) {
        console.log("storage.error", error)
    } finally {
        console.log("storage.finally")
    }
}

function checkSetting() {
    chrome.storage.local.get(["enabled"], (setting) => {
        console.log("storage.get.setting", setting)
        const isEnabled = setting.enabled
        const settingCheckbox = document.getElementById("darkSetting") as HTMLInputElement
        settingCheckbox.checked = isEnabled

        if (isEnabled) {
            enableDarkMode()
        }
    })
}

// 扩展内部通信
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
    console.log("Content", message)
    console.log("Content", sender)
    sendResponse({name: "Agent"})
})

// 与外部资源通信
class OllamaChat {
    model: string
    apiUrl: string
    
    constructor(model="glm4:9b", apiUrl="http://127.0.0.1:11434/api/chat") {
        this.model = model
        this.apiUrl = apiUrl
    }
    async invoke(message: string) {
        try {
            let response = await fetch(this.apiUrl, {
                method: "POST",
                headers: {
                    "Content-Type": "application/json"
                },
                body: JSON.stringify({
                    model: this.model,
                    messages: [
                        {role: "user", content: message}
                    ],
                    stream: false,
                })
            })
            let data = await response.json()
            console.log(`${data.message.role}: ${data.message.content}`)
        } catch (error) {
            console.log("Chat Error", error)
        } finally {
            // console.log("Chat Finally")
        }
    }
}

const chat = new OllamaChat()
// chat.invoke("你好")
